package org.demo.controller;

import jakarta.validation.Valid;
import org.demo.common.Result;
import org.demo.entity.Houses;
import org.demo.entity.HousesDto;
import org.demo.entity.PageBean;
import org.demo.mapper.HousesMapper;
import org.demo.service.IHousesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


//房屋表
@RestController
@RequestMapping("/houses")
public class HousesConteoller {
    @Autowired
    private IHousesService housesService;
    @Autowired
    private HousesMapper housesMapper;

    @PostMapping("/add")
    public Result add(@RequestBody HousesDto housesDto){
        if (!StringUtils.hasLength(housesDto.getCommunityId()) ||
                !StringUtils.hasLength(housesDto.getDescription()) ||
                !StringUtils.hasLength(housesDto.getAddress()) ||
                housesDto.getArea()==null ||
                housesDto.getPrice()==null ||
                !StringUtils.hasLength(housesDto.getStatus()) ||
                !StringUtils.hasLength(housesDto.getType()) ||
                !StringUtils.hasLength(housesDto.getLandlordId())
                ){
            return Result.error("缺少必要的参数");
        }
        housesService.add(housesDto);
        return Result.success();
    }

    @PostMapping("/addLanHouse")
    public Result addLanHouse(@RequestBody HousesDto housesDto){
        if (!StringUtils.hasLength(housesDto.getCommunityId()) ||
                !StringUtils.hasLength(housesDto.getDescription()) ||
                !StringUtils.hasLength(housesDto.getAddress()) ||
                housesDto.getArea()==null ||
                housesDto.getPrice()==null)
        {
            return Result.error("缺少必要的参数");
        }
        housesService.addLanHouse(housesDto);
        return Result.success();
    }


    @PostMapping("/list")
    public Result<PageBean<HousesDto>> Pagelist(Integer pageNum,
                                         Integer pageSize,
                                         @RequestBody (required = false) HousesDto houses){
        PageBean<HousesDto> pb= housesService.Pagelist(houses.getPageNum(),houses.getPageSize(),houses);
        return Result.success(pb);
    }

    @PostMapping("/selectId")
    public Result<HousesDto> selectId(@RequestBody (required = false) HousesDto houses){
        HousesDto pb= housesService.selectId(houses);
        return Result.success(pb);
    }

    @GetMapping("/houselist")
    public Result<List<HousesDto>> houselist(@RequestBody (required = false) HousesDto houses){
        List<HousesDto> pb= housesService.list(houses);
        return Result.success(pb);
    }

    @PostMapping("/buylist")
    public Result<PageBean<HousesDto>> buylist(Integer pageNum,
                                                Integer pageSize,
                                                @RequestBody (required = false) HousesDto houses){
        PageBean<HousesDto> pb= housesService.buylist(houses.getPageNum(),houses.getPageSize(),houses);
        return Result.success(pb);
    }
    @PostMapping("/buyCuslist")
    public Result<PageBean<HousesDto>> buyCuslist(Integer pageNum,
                                               Integer pageSize,
                                               @RequestBody (required = false) HousesDto houses){
        PageBean<HousesDto> pb= housesService.buyCuslist(houses.getPageNum(),houses.getPageSize(),houses);
        return Result.success(pb);
    }
    @PostMapping("/resCuslist")
    public Result<PageBean<HousesDto>> resCuslist(Integer pageNum,
                                               Integer pageSize,
                                               @RequestBody (required = false) HousesDto houses){
        PageBean<HousesDto> pb= housesService.resCuslist(houses.getPageNum(),houses.getPageSize(),houses);
        return Result.success(pb);
    }

    @PostMapping("/reslist")
    public Result<PageBean<HousesDto>> reslist(Integer pageNum,
                                               Integer pageSize,
                                               @RequestBody (required = false) HousesDto houses){
        PageBean<HousesDto> pb= housesService.reslist(houses.getPageNum(),houses.getPageSize(),houses);
        return Result.success(pb);
    }

    @GetMapping("/detalil")
    public Result<Houses> detalil(String id){
        Houses houses = housesService.detalil(id);
        return Result.success(houses);
    }
    @PutMapping("/update")
    public Result update(@RequestBody HousesDto houses){
        if (!StringUtils.hasLength(houses.getCommunityId()) ||
                !StringUtils.hasLength(houses.getDescription()) ||
                !StringUtils.hasLength(houses.getAddress()) ||
                houses.getArea()==null ||
                houses.getPrice()==null ||
                !StringUtils.hasLength(houses.getStatus()) ||
                !StringUtils.hasLength(houses.getType()) ||
                !StringUtils.hasLength(houses.getLandlordId())
        ){
            return Result.error("缺少必要的参数");
        }
        housesService.update(houses);
        return Result.success();
    }
    @PostMapping("/downHouse")
    public Result downHouse(@RequestBody Houses houses){
        Houses house = housesMapper.selectById(houses.getId());
        if (house.getStatus().equals("2")){
            return Result.error("已下架");
        }if (house.getStatus().equals("3")){
            return Result.error("已完成交易");
        }

        housesService.downHouse(houses);
        return Result.success();
    }

    @PostMapping("/ItemAllupdate")
    public Result ItemAllupdate(@RequestBody HousesDto houses){
        if (!StringUtils.hasLength(houses.getCommunityId()) ||
                !StringUtils.hasLength(houses.getDescription()) ||
                !StringUtils.hasLength(houses.getAddress()) ||
                houses.getArea()==null ||
                houses.getPrice()==null ||
                !StringUtils.hasLength(houses.getStatus()) ||
                !StringUtils.hasLength(houses.getType()) ||
                !StringUtils.hasLength(houses.getLandlordId())
        ){
            return Result.error("缺少必要的参数");
        }
        housesService.ItemAllupdate(houses);
        return Result.success();
    }


    @DeleteMapping("/delete")
    public  Result delete(String id){
        housesService.delete(id);
        return Result.success();
    }

    @PostMapping("/weituoOK")
    public Result weituoOK(@RequestBody Houses houses){
        Houses houses1 = housesMapper.selectById(houses.getId());
        if (!houses1.getStatus().equals("0")){
            return Result.error("房屋状态异常");
        }

        housesService.weituoOK(houses);
        return Result.success();
    }



}
